package org.parademo.service;

import org.parademo.dao.ProposalDao;
import org.parademo.model.ProposalEntity;
import org.parademo.model.TopicEntity;
import org.parademo.model.UserEntity;
import org.parademo.service.model.ProposalStatusCode;
import org.parademo.service.security.ParademoUser;
import org.parademo.service.security.SecurityUtil;
import org.parademo.web.model.ProposalFormView;

import java.util.Date;
import java.util.List;

public class ProposalServiceImpl implements ProposalService {
    private final ProposalDao proposalDao;
    private final ProposalStatusService proposalStatusService;

    public ProposalServiceImpl(ProposalDao proposalDao, ProposalStatusService proposalStatusService) {
        this.proposalDao = proposalDao;
        this.proposalStatusService = proposalStatusService;
    }

    @Override
    public List<ProposalEntity> getProposalsForUser(long userId) {
        return proposalDao.getProposalsForUser(userId);
    }

    @Override
    public ProposalFormView getProposalById(long proposalId) {
        ProposalEntity proposal = proposalDao.retrieve(proposalId);

        // todo: where to put this copying code?
        ProposalFormView result = new ProposalFormView();
        result.setId(
                proposal.getId()
        );
        result.setTitle(
                proposal.getTitle()
        );
        TopicEntity topic = proposal.getTopic();
        result.setTopicId(
                topic.getId()
        );
        result.setTopic(
                topic.getName()
        );

        result.setVotingEndDate(
                proposal.getVotingEndDate()
        );

        result.setDescription(
                proposal.getDescription()
        );

        // all other codes except CREATED
        result.setSubmitted(
                !ProposalStatusCode.CREATED.name().equals(proposal.getStatus().getCode())
        );

        return result;
    }

    @Override
    public void createProposal(ProposalFormView proposal) {
        ProposalEntity proposalEntity = new ProposalEntity();

        proposalEntity.setTitle(
                proposal.getTitle()
        );
        proposalEntity.setDescription(
                proposal.getDescription()
        );
        proposalEntity.setTopic(
                getTopic(
                        proposal.getTopicId()
                )
        );
        proposalEntity.setAuthor(
                getCurrentUser()
        );

        proposalEntity.setStatus(
                proposalStatusService.getByCode(ProposalStatusCode.CREATED)
        );

        proposalEntity.setVotingStartDate(new Date());
        proposalEntity.setVotingEndDate(
                proposal.getVotingEndDate()
        );

        proposalDao.create(proposalEntity);

    }

    private UserEntity getCurrentUser() {
        ParademoUser currentUser = SecurityUtil.getCurrentUser();
        Long currentUserId = currentUser.getId();

        UserEntity userEntity = new UserEntity();
        userEntity.setId(currentUserId);

        return userEntity;
    }

    private TopicEntity getTopic(Long topicId) {
        TopicEntity topic = new TopicEntity();

        topic.setId(topicId);

        return topic;
    }

}
